import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.folderlistmodel
import "./Utils.js" as Utils
import BNSPlayer

Rectangle {
    height: 720
    width: 512
    border.color: "#444444"
    border.width: 2
    radius: 10
    id: rootItem
    color: "#333333"

    property url currentFile

    property var filesMap: ({})

    onCurrentFileChanged: {
        filesMap = {}
        if (mediaInspector.openDir(Utils.dirFromURL(currentFile)) < 0) {
            stateLabel.text = qsTr("无法打开目录")
        } else {
            stateLabel.text = ""
        }
    }

    MediaInspector {
        id: mediaInspector

        onRowsInserted: (par, first, last) => {
                            for (var i = first; i <= last; ++i) {
                                var fileUrl = Utils.localPathToURL(mediaInspector.filename(i))
                                var lastTimepoint = Utils.playHistory(fileUrl)
                                if (lastTimepoint !== 0.0) {
                                    setLastTimepoint(i, lastTimepoint)
                                }

                                filesMap[fileUrl] = i;
                            }
                        }

        onScanFinished: {

            console.log("onScanFinished: ", currentFile)

            if (currentFile in filesMap) {
                var i = filesMap[currentFile]
                listView.currentIndex = i
            }
        }
    }

    ListModel {
        id: historyModel
    }

    Rectangle {
        anchors.fill: parent
        anchors.margins: 10
        color: parent.color
        id: errorPanel

        visible: stateLabel.text !== ""

        Text {
            id: stateLabel
            color: "#efefef"
            anchors.centerIn: parent
        }
    }

    ListView {
        anchors.fill: parent
        anchors.margins: 10
        model: mediaInspector
        visible: !errorPanel.visible
        id: listView

        delegate: Rectangle {

            required property real duration
            required property real lastTimepoint
            required property string titleImg
            required property string name
            required property int index

            height: 100
            width: listView.width
            color: itemMouseArea.pressed ? "#222222" : (itemMouseArea.hovered ? "#444444" : (listView.currentIndex === index ? Qt.lighter("#333333", 1.2) : "#333333"))

            border.width: listView.currentIndex === index ? 2 : 0
            radius: border.width
            border.color: border.width === 2 ? Qt.lighter(color, 1.3) : color

            Image {
                id: titleImage
                source: titleImg === "" ? "../images/noImage.png" : Utils.localPathToURL(titleImg);
                sourceSize.width: 90
                sourceSize.height: 90
                x: 5
                width: 90
                height: 90
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: name
                color: "#efefef"
                font.pixelSize: 15
                width: parent.width - titleImage.width - 10 - durationLabel.width - 10 - 20
                height: 15
                elide: Text.ElideRight
                anchors.topMargin: 10
                anchors.left: titleImage.right
                anchors.leftMargin: 10
                y: parent.height / 2 - height / 2
                verticalAlignment: Text.AlignVCenter
            }

            Text {
                id: durationLabel
                anchors.right: parent.right
                anchors.rightMargin: 10
                y: parent.height / 2 - height / 2
                //width: parent.width / 2
                height: 14
                text: Utils.timeToString(duration)
                color: "#efefef"
                font.pixelSize: 14
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
            }

            Text {
                anchors.top: durationLabel.bottom
                anchors.topMargin: 10
                anchors.right: parent.right
                anchors.rightMargin: 10
                color: "#bfbfbf"
                text: lastTimepoint === 0.0 ? "" : "上次看到: " + Utils.timeToString(
                                                  lastTimepoint)
            }

            MouseArea {
                id: itemMouseArea
                anchors.fill: parent
                hoverEnabled: true
                property bool hovered: false
                onEntered: hovered = true
                onExited: hovered = false
                onPressed: {
                    listView.currentIndex = parent.index
                    var filename = mediaInspector.filename(listView.currentIndex)
                    if (filename !== "" && filename !== Utils.urlToLocalFile(currentFile)) {
                        currentFile = Utils.localPathToURL(filename);
                    }
                }
            }

            Behavior on height {
                NumberAnimation {
                    duration: 300 // 动画持续时间
                    easing.type: Easing.InOutCubic
                }
            }
        }
    }
}
